// api_response.dart中的LoginData已经在user.dart中定义，这里不需要重复定义

// 统一API响应模型
class ApiResponse<T> {
  final bool success;
  final String message;
  final T? data;
  final dynamic error;
  final int? code;

  ApiResponse({
    required this.success,
    required this.message,
    this.data,
    this.error,
    this.code,
  });

  factory ApiResponse.fromJson(Map<String, dynamic> json, T? Function(dynamic)? fromData) {
    return ApiResponse<T>(
      success: json['success'] ?? false,
      message: json['message'] ?? '',
      data: fromData != null && json['data'] != null ? fromData(json['data']) : json['data'],
      error: json['error'],
      code: json['code'],
    );
  }

  // 成功响应构造函数
  factory ApiResponse.success(T data, {String? message}) {
    return ApiResponse<T>(
      success: true,
      message: message ?? '操作成功',
      data: data,
    );
  }

  // 错误响应构造函数
  factory ApiResponse.error(String message, {dynamic error, int? code}) {
    return ApiResponse<T>(
      success: false,
      message: message,
      error: error,
      code: code,
    );
  }

  // 网络错误响应构造函数
  factory ApiResponse.networkError(String errorMessage) {
    return ApiResponse<T>(
      success: false,
      message: '网络错误：$errorMessage',
      error: errorMessage,
    );
  }
}

// 分页响应数据模型
class PaginationData<T> {
  final List<T> items;
  final int total;
  final int page;
  final int pageSize;
  final int totalPages;

  PaginationData({
    required this.items,
    required this.total,
    required this.page,
    required this.pageSize,
    required this.totalPages,
  });

  factory PaginationData.fromJson(
    Map<String, dynamic> json,
    T Function(Map<String, dynamic>) fromJsonT,
  ) {
    return PaginationData<T>(
      items: (json['items'] as List<dynamic>?)
          ?.map((item) => fromJsonT(item as Map<String, dynamic>))
          .toList() ?? [],
      total: json['total'] ?? 0,
      page: json['page'] ?? 1,
      pageSize: json['page_size'] ?? 20,
      totalPages: json['total_pages'] ?? 1,
    );
  }
}

// 验证码发送响应数据
class SendCodeData {
  final String verificationId;
  final int expiresIn;

  SendCodeData({
    required this.verificationId,
    required this.expiresIn,
  });

  factory SendCodeData.fromJson(Map<String, dynamic> json) {
    return SendCodeData(
      verificationId: json['verification_id'] ?? '',
      expiresIn: json['expires_in'] ?? 300,
    );
  }
}

// LoginData已经在models/user.dart中定义，这里移除重复定义
